<?php

namespace app\admin\validate;

use think\Validate;

class Communityapply extends Validate
{

    /**
     * 验证规则
     */
    protected $rule = [
        'title' => 'require|length:2,100',
        'start_time' => 'require|date',
        'end_time' => 'require|date',
        'quota' => 'require|integer|gt:0',
        'content' => 'length:0,5000',
        'cover' => 'length:0,255',
        'is_class_limit' => 'in:0,1',
        'status' => 'in:0,1',
    ];

    /**
     * 提示消息
     */
    protected $message = [
        'title.require' => '项目名称不能为空',
        'title.length' => '项目名称长度必须在2-100个字符之间',
        'start_time.require' => '活动开始时间不能为空',
        'start_time.date' => '活动开始时间格式不正确',
        'end_time.require' => '活动结束时间不能为空',
        'end_time.date' => '活动结束时间格式不正确',
        'quota.require' => '名额不能为空',
        'quota.integer' => '名额必须是整数',
        'quota.gt' => '名额必须大于0',
        'content.length' => '活动简介长度不能超过5000个字符',
        'cover.length' => '封面图路径长度不能超过255个字符',
        'is_class_limit.in' => '是否指定班级参数错误',
        'status.in' => '状态参数错误',
    ];

    /**
     * 字段描述
     */
    protected $field = [
        'title' => '项目名称',
        'start_time' => '活动开始时间',
        'end_time' => '活动结束时间',
        'quota' => '名额',
        'content' => '活动简介',
        'cover' => '封面图',
        'is_class_limit' => '是否指定班级',
        'status' => '状态',
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add' => [
            'title' => 'require|length:2,100',
            'start_time' => 'require|date',
            'end_time' => 'require|date',
            'quota' => 'require|integer|gt:0',
            'content' => 'length:0,5000',
            'cover' => 'length:0,255',
            'is_class_limit' => 'in:0,1',
            'status' => 'in:0,1',
        ],
        'edit' => [
            'title' => 'require|length:2,100',
            'start_time' => 'require|date',
            'end_time' => 'require|date',
            'quota' => 'require|integer|gt:0',
            'content' => 'length:0,5000',
            'cover' => 'length:0,255',
            'is_class_limit' => 'in:0,1',
            'status' => 'in:0,1',
        ],
    ];

    /**
     * 自定义验证方法：验证结束时间必须大于开始时间
     */
    protected function checkEndTime($value, $rule, $data)
    {
        if (isset($data['start_time']) && $data['start_time']) {
            $startTime = strtotime($data['start_time']);
            $endTime = strtotime($value);
            
            if ($endTime <= $startTime) {
                return '活动结束时间必须大于开始时间';
            }
        }
        return true;
    }
}